-- equip
-- create by cheny3
-- 替换装备

return {
    -- 判断
    canTrigger = function(args)
        return true;
    end,

    -- 触发
    trigger = function(args)
        -- 获取玩家当前有的装备
        local equips = EquipM.getUserTreasureList();

        -- 身上穿的装备部位
        local equipType = { EQUIP_TYPE_TRUMP, EQUIP_TYPE_RING, EQUIP_TYPE_NECKLACE,
                            EQUIP_TYPE_HELMET, EQUIP_TYPE_CLOAK, EQUIP_TYPE_ARMOR,
                            EQUIP_TYPE_GLOVE, EQUIP_TYPE_WEAPON, EQUIP_TYPE_MEDAL,
                            EQUIP_TYPE_BOOT, EQUIP_TYPE_BELT, EQUIP_TYPE_RISKER};

        for i = 1, 12 do
            -- 相当于continue
            while true do
                local classId = EquipM.getEquip(equipType[i], 1);
                local info    = EquipM.query(classId,"dbase");

                -- 当前装备的是神器、圣器、神灯、优先装备的装备
                if info then
                    if ArtifactM.isArtifact(classId) or HolyItemsM.isHolyItems(classId) or
                        info["aladdin_lamp"] == 1 or info["priority"] == 1 then
                        break;
                    end
                end

                local rank = EquipM.query(classId, "rank");
                for classId, amount in pairs(equips) do
                    -- 因为euqips包含装备的武器，所以这么处理
                    local rank1 = EquipM.query(classId, "rank");
                    if ItemM.getAmount(ME.user, classId) > 0 and EquipM.query(classId, "type") == equipType[i] then
                        -- 该装备属于圣器、神灯、优先装备的装备
                        info = EquipM.query(classId,"dbase");
                        if HolyItemsM.isHolyItems(classId) or info["aladdin_lamp"] == 1 or
                           info["priority"] == 1 or rank < rank1 then
                            return DungeonActionM.go("do_equip", 0, classId);
                        end
                    end
                end
                break;
            end
        end

        return false;
    end,
};
